<script setup lang='ts'>
import { doToDoFenMiao, duToDoFen, duFenMiaoToDu, duFenToDo } from "./base/duTransform"
import { ref, watch } from 'vue';
const props = withDefaults(defineProps<{
    positionValue?: [number, number, number],
    lonLatFormat: string
}>(), { positionValue: () => [0, 0, 0], lonLatFormat: '' })//lonLatFormat: 'SEXAGESIMAL_DEGREE' ||'DEGREES_DECIMAL_MINUTES'
type defaultValueType = [number, number, number]

const emits = defineEmits([
    "update:positionValue"
]);
//度
const longitude = ref()//经度
const latitude = ref()//纬度
const height = ref()
//度分秒
const duFenMiaoLongitud = ref<[number, number, number]>([0, 0, 0])//经度
const duFenMiaoLatitude = ref<[number, number, number]>([0, 0, 0])//纬度
//度分
const duFenLongitude = ref<[number, number]>([0, 0])//经度
const duFenLatitude = ref<[number, number]>([0, 0])//纬度

watch(() => props.positionValue, (val) => {
    if (props.lonLatFormat === 'SEXAGESIMAL_DEGREE') {
        duFenMiaoLongitud.value = doToDoFenMiao(val[0])
        duFenMiaoLatitude.value = doToDoFenMiao(val[1])
    } else if (props.lonLatFormat === 'DEGREES_DECIMAL_MINUTES') {
        duFenLongitude.value = duToDoFen(val[0])
        duFenLatitude.value = duToDoFen(val[1])
    } else {
        longitude.value = val[0].toFixed(7)
        latitude.value = val[1].toFixed(7)
    }
    height.value = Number(val[2].toFixed(3))

}, { immediate: true })
//度
const update1 = (e: any, numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    numberArr[num] = Number(e.target.value)
    emits("update:positionValue", numberArr)
}
//度分秒
const update2 = (numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenMiaoToDu(duFenMiaoLongitud.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenMiaoToDu(duFenMiaoLatitude.value)
        numberArr[num] = du
    }
    emits("update:positionValue", numberArr)
}
//度分
const update3 = (numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenToDo(duFenLongitude.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenToDo(duFenLatitude.value)
        numberArr[num] = du
    }
    emits("update:positionValue", numberArr)
}

</script>

<template>

    <div class="position" v-if="lonLatFormat === 'SEXAGESIMAL_DEGREE'">
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '经度' }}</label>
            <div class="point_dufenmiao">
                <input type="number" class="input_number" v-model="duFenMiaoLongitud[0]" @blur="update2(positionValue, 0)"
                    @keydown.enter="update2(positionValue, 0)" placeholder="请输入" /><span>{{
        '°' }}</span>
                <input type="number" class="input_number" v-model="duFenMiaoLongitud[1]" @blur="update2(positionValue, 0)"
                    @keydown.enter="update2(positionValue, 0)" placeholder="请输入" /><span>{{
        "'" }}</span>
                <input type="number" class="input_number" v-model="duFenMiaoLongitud[2]" @blur="update2(positionValue, 0)"
                    @keydown.enter="update2(positionValue, 0)" placeholder="请输入" /><span>{{
        '"' }}</span>
            </div>
        </div>
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '纬度' }}</label>
            <div class="point_dufenmiao">
                <input type="number" class="input_number" v-model="duFenMiaoLatitude[0]" @blur="update2(positionValue, 1)"
                    @keydown.enter="update2(positionValue, 1)" placeholder="请输入" /><span>{{
        '°' }}</span>
                <input type="number" class="input_number" v-model="duFenMiaoLatitude[1]" @blur="update2(positionValue, 1)"
                    @keydown.enter="update2(positionValue, 1)" placeholder="请输入" /><span>{{
        "'" }}</span>
                <input type="number" class="input_number" v-model="duFenMiaoLatitude[2]" @blur="update2(positionValue, 1)"
                    @keydown.enter="update2(positionValue, 1)" placeholder="请输入" /><span>{{
        '"' }}</span>
            </div>
        </div>
        <div class="input2">
            <label for=""> {{ '高度' }}</label>
            <input type="number" class="input_number" :value="height" @blur="update1($event, positionValue, 2)"
                @keydown.enter="update1($event, positionValue, 2)" step="0.001" placeholder="请输入高度" />
        </div>
    </div>
    <div class="position" v-else-if="lonLatFormat === 'DEGREES_DECIMAL_MINUTES'">
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '经度' }}</label>
            <div class="point_dufen">
                <input type="number" class="input_number" v-model="duFenLongitude[0]" @blur="update3(positionValue, 0)"
                    @keydown.enter="update3(positionValue, 0)" placeholder="请输入" /><span>{{
        '°' }}</span>
                <input type="number" class="input_number" v-model="duFenLongitude[1]" @blur="update3(positionValue, 0)"
                    @keydown.enter="update3(positionValue, 0)" placeholder="请输入" /><span>{{
        "'" }}</span>
            </div>
        </div>
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '纬度' }}</label>
            <div class="point_dufen">
                <input type="number" class="input_number" v-model="duFenLatitude[0]" @blur="update3(positionValue, 1)"
                    @keydown.enter="update3(positionValue, 1)" placeholder="请输入" /><span>{{
        '°' }}</span>
                <input type="number" class="input_number" v-model="duFenLatitude[1]" @blur="update3(positionValue, 1)"
                    @keydown.enter="update3(positionValue, 1)" placeholder="请输入" /><span>{{
        "'" }}</span>
            </div>
        </div>
        <div class="input2">
            <label for=""> {{ '高度' }}</label>
            <input type="number" class="input_number" :value="height" @blur="update1($event, positionValue, 2)"
                @keydown.enter="update1($event, positionValue, 2)" step="0.001" placeholder="请输入高度" />
        </div>
    </div>
    <div class="position" v-else>
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '经度' }}</label>
            <input type="number" class="input_number" :value="longitude" @blur="update1($event, positionValue, 0)"
                @keydown.enter="update1($event, positionValue, 0)" step="0.0000001" placeholder="请输入经度" />
            <span style="position: absolute;right: 5px;top: 2px;">°</span>

        </div>
        <div class="input2" style="margin-bottom: 6px;">
            <label for=""> {{ '纬度' }}</label>
            <input type="number" class="input_number" :value="latitude" @blur="update1($event, positionValue, 1)"
                @keydown.enter="update1($event, positionValue, 1)" step="0.0000001" placeholder="请输入纬度" />
            <span style="position: absolute;right: 5px;top: 2px;">°</span>
        </div>
        <div class="input2">
            <label for=""> {{ '高度' }}</label>
            <input type="number" class="input_number" :value="height" @blur="update1($event, positionValue, 2)"
                @keydown.enter="update1($event, positionValue, 2)" step="0.001" placeholder="请输入高度" />
        </div>
    </div>
</template>

<style scoped>
.position {
    width: 100%;
    border-top: 1px solid #111111;
    border-bottom: 1px solid #111111;
    padding-top: 10px;
    padding-bottom: 10px;
}

.input2 {
    width: 100%;
    display: flex;
    align-items: center;
    font-size: 14px;
    line-height: 26px;
    color: rgba(230, 230, 230, 1);
    position: relative;
}

.input2>label {
    display: inline-block;
    width: 70px;
    height: 26px;
    font-family: SourceHanSansCN, SourceHanSansCN;
    font-weight: 400;
    text-align: left;
}

.input2>input {
    display: inline-block;
    /* width: 190px; */
    flex: 1;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>.point_dufenmiao {
    display: flex;
    flex: 1;
    height: 26px;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
}

.input2>.point_dufenmiao>input {
    display: inline-block;
    width: 30.3%;
    /* flex: 1; */
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>.point_dufenmiao>span {
    display: inline-block;
    width: 3%;
}

.input2>.point_dufen {
    display: flex;
    flex: 1;
    height: 26px;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
}

.input2>.point_dufen>input {
    display: inline-block;
    width: 47%;
    /* flex: 1; */
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.input2>.point_dufen>span {
    display: inline-block;
    width: 3%;
}

.input2>input:hover {
    border: 1px solid rgba(44, 104, 247, 1);
}

.input2>input:focus {
    border: 1px solid #2c68f7;
}

.input_number::-webkit-outer-spin-button,
.input_number::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.input_number[type="number"] {
    -moz-appearance: textfield;
}
</style>
